package hos.utils;

import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Parcelable;
import android.util.Size;
import android.util.SizeF;

import java.io.Serializable;
import java.lang.reflect.Array;

/**
 * <p>Title: BundleUtils </p>
 * <p>Description:  </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @version : 1.0
 * @date : 2023/4/17 23:25
 */
public class BundleUtils {

    @SafeVarargs
    @SuppressWarnings("ConstantConditions")
    public static Bundle convert(android.util.Pair<String, Object>... pairs) {
        Bundle bundle = new Bundle(pairs.length);
        for (android.util.Pair<String, Object> pair : pairs) {
            String key = pair.first;
            Object value = pair.second;
            if (value == null) {
                bundle.putString(key, null);
            } else if (value instanceof String) {
                bundle.putString(key, (String) value);
            } else if (value instanceof Boolean) {
                bundle.putBoolean(key, (Boolean) value);
            } else if (value instanceof Byte) {
                bundle.putByte(key, (byte) value);
            } else if (value instanceof Character) {
                bundle.putChar(key, (Character) value);
            } else if (value instanceof Double) {
                bundle.putDouble(key, (Double) value);
            } else if (value instanceof Float) {
                bundle.putFloat(key, (Float) value);
            } else if (value instanceof Integer) {
                bundle.putInt(key, (Integer) value);
            } else if (value instanceof Long) {
                bundle.putLong(key, (Long) value);
            } else if (value instanceof Short) {
                bundle.putShort(key, (Short) value);
                // References
            } else if (value instanceof Bundle) {
                bundle.putBundle(key, (Bundle) value);
            } else if (value instanceof CharSequence) {
                bundle.putCharSequence(key, (CharSequence) value);
            } else if (value instanceof Parcelable) {
                bundle.putParcelable(key, (Parcelable) value);
                // Scalar arrays
            } else if (value instanceof Boolean[]) {
                bundle.putBooleanArray(key, (boolean[]) value);
            } else if (value instanceof Byte[]) {
                bundle.putByteArray(key, (byte[]) value);
            } else if (value instanceof Character[]) {
                bundle.putCharArray(key, (char[]) value);
            } else if (value instanceof Double[]) {
                bundle.putDoubleArray(key, (double[]) value);
            } else if (value instanceof Float[]) {
                bundle.putFloatArray(key, (float[]) value);
            } else if (value instanceof Integer[]) {
                bundle.putIntArray(key, (int[]) value);
            } else if (value instanceof Long[]) {
                bundle.putLongArray(key, (long[]) value);
            } else if (value instanceof Short[]) {
                bundle.putShortArray(key, (short[]) value);
                // Reference arrays
            } else if (value instanceof Array) {
                Class<?> componentType = value.getClass().getComponentType();
                if (Parcelable.class.isAssignableFrom(componentType)) {
                    bundle.putParcelableArray(key, (Parcelable[]) value);
                } else if (String.class.isAssignableFrom(componentType)) {
                    bundle.putStringArray(key, (String[]) value);
                } else if (CharSequence.class.isAssignableFrom(componentType)) {
                    bundle.putCharSequenceArray(key, (CharSequence[]) value);
                } else if (Serializable.class.isAssignableFrom(componentType)) {
                    bundle.putSerializable(key, (Serializable[]) value);
                } else {
                    String valueType = componentType.getCanonicalName();
                    throw new IllegalArgumentException(
                            "Illegal value array type " + valueType + " for key " + key + ""
                    );
                }
            } else if (value instanceof Parcelable[]) {
                bundle.putParcelableArray(key, (Parcelable[]) value);
            } else if (value instanceof String[]) {
                bundle.putStringArray(key, (String[]) value);
            } else if (value instanceof CharSequence[]) {
                bundle.putCharSequenceArray(key, (CharSequence[]) value);
            } else if (value instanceof Serializable[]) {
                bundle.putSerializable(key, (Serializable[]) value);
            } else if (value instanceof Serializable) {
                bundle.putSerializable(key, (Serializable) value);
            } else {
                if (Build.VERSION.SDK_INT >= 18 && value instanceof IBinder) {
                    bundle.putBinder(key, (IBinder) value);
                } else if (Build.VERSION.SDK_INT >= 21 && value instanceof Size) {
                    bundle.putSize(key, (Size) value);
                } else if (Build.VERSION.SDK_INT >= 21 && value instanceof SizeF) {
                    bundle.putSizeF(key, (SizeF) value);
                } else {
                    String valueType = value.getClass().getCanonicalName();
                    throw new IllegalArgumentException(
                            "Illegal value array type " + valueType + " for key " + key + ""
                    );
                }
            }
        }
        return bundle;
    }
}
